// app.js
const express = require('express')
const fs = require('fs')
const getComments = require('./utils/getComments')
const writeComments = require('./utils/writeComments')
const app = express()

app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.use(express.static('public'))

app.get('/', (req, res) => {
    const html = fs.readFileSync('./views/index.html', 'utf-8')
    res.end(html)
})

app.post('/', async (req, res) => {
    const {comment} = req.body
    await writeComments(comment)
    res.json()
})

app.get('/api',async (req, res) => {
    // api = getComments 时候为获得comments数据
    const { api } = req.query
    if (api === 'getComments') {
        res.json(await getComments())
    }
})

app.listen(3000)